package com.itheima.reggie_auto.controller;


import com.itheima.reggie_auto.common.R;
import com.itheima.reggie_auto.entity.ShoppingCart;
import com.itheima.reggie_auto.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 刘少宽
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> list= shoppingCartService.getAll();
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart shopping = shoppingCartService.add(shoppingCart);
        return R.success(shopping);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppingCartService.clean();

        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.sub(shoppingCart);

        return R.success("删除成功");
    }

}

